import React from 'react'
// ===================================================================== Temp
import Controls from '#cpt/temp/Controls'
// ===================================================================== declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Form 		= $Lazy.hook(()=>import('@antd/form/layout/form'))
const FormItem 	= $Lazy.load(()=>import('#cpt/form/submit-item'))
const Box 		= $Lazy.load(()=>import('#tp/box/box'))
const IsUpload 	= $Lazy.load(()=>import('./cpt/is-upload'))
const Submit 	= $Lazy.file(()=>import('#cpt/temp/js/onSubmit'))
const Title 	= $Lazy.load(()=>import('#tp/title/title-small'))
const message 	= $Lazy.file(()=>import('@antd/message'))
// ===================================================================== component
// ===================================================================== component
const Index = ({ rows, mode, disabled, isFirst, info={}, submitLoading, onRefresh }) => {
	const formRef = React.useRef()
	// 44015
	const [ inoculate, setInoculate ] = React.useState()	// 是否接种
	const [ harvest, setHarvest ] = React.useState()		// 是否收获
	// 条件判断：默认 FL104210000
	const _shou_show = mode === 44015 ? (info.operation_44015 === 'FL104210001' || info.operation_44015 === 'FL104210002') : null	// 显示收获
	const _result_show = mode === 44015 ? info.operation_44015 === 'FL104210002' : null												// 显示结果
	
	// 44018
	const [ before, setBefore, ] = React.useState()			// 是否前处理
	const [ hybrid, setHybrid ] = React.useState()			// 是否杂交
	const [ after, setAfter ] = React.useState()			// 是否后处理
	// 条件判断：默认 FL1000010000
	const _hybrid_show = mode === 44018 ? info.operation_44018 === 'FL1000010001' || info.operation_44018 === 'FL1000010002'  || info.operation_44018 === 'FL1000010007' : null											// 显示杂交
	const _after_show = mode === 44018 ? info.operation_44018 === 'FL1000010002' || info.operation_44018 === 'FL1000010007' : null												// 显示后处理
	const _rs_show = mode === 44018 ? info.operation_44018 === 'FL1000010007' : null												// 显示结果
	
	React.useEffect( () => {
		setTimeout(()=>{
			if(mode === 44015){
				setInoculate(_shou_show)
				setHarvest(_result_show)
			}else if(mode === 44018){
				setBefore(_hybrid_show)
				setHybrid(_after_show)
				setAfter(_rs_show)
			}
			
			const { content } = info
			formRef.current?.refs?.ref?.reset()
			if(formRef.current && $fn.hasObject(content)){
				formRef.current.refs.ref?.setValue(content)
			}
		})
	},[info, mode, 
		_shou_show, _result_show,	// 44015
		_hybrid_show, _after_show, _rs_show // 44018
	])
	
	// 操作按钮
	const controls = React.useCallback(() => {
		let _disabled = disabled
		if(mode === 44015){
			_disabled = disabled || (!_shou_show && !inoculate) || (_shou_show && !harvest)
		}else if(mode === 44018){
			_disabled = disabled || (!_hybrid_show && !before) || (_hybrid_show && !hybrid) || (_after_show && !after)
		}
		return [
			Controls.click('保存', () => { formRef.current.refs.ref.submit() },{
				disabled: _disabled,
				code:'F2',
				tip: () => $fn.hasObject(info) ? true : '请先选择项目列表一条数据'
			})
		]
	},[	info, disabled, mode, 
		inoculate, harvest, _shou_show,	// 44015
		before, after, hybrid, _after_show, _hybrid_show
	])
	
	const onFinish = React.useCallback( (param, flag) => {
		const { status, result_model, uuid } = info
		Submit.then(f => f({ msg:'保存', api:'result-unit-item/update', param:{ dmodel:result_model, status: status, uuid, ...param }, onRefresh:flag?onRefresh:null }))
	},[onRefresh, info])
	
	const onSubmit = React.useCallback( params => {
		if(mode === 44015){
			const { operation_44015 } = info
			let param = { content:{ operation_44015 } }
			
			if(!_shou_show && !_result_show){
				if(!inoculate) return message.then(f=>f.warning('请确认是否接种完成'))
				else onFinish(param, true)
			}else if( _shou_show && !_result_show ){
				if(!harvest) return message.then(f=>f.warning('请确认是否收获完成'))
				else onFinish(param, true)
			}else if(_result_show){
				onFinish( { ...param, content:{operation_44015, ...params}} )
			}
		}else if(mode === 44018){
			const { operation_44018 } = info
			let param = { content:{ operation_44018 } }
			if(!_hybrid_show && !_after_show && !_rs_show){
				if(!before) return message.then(f=>f.warning('请确认是否前处理完成'))
				else onFinish(param, true)
			}else if( _hybrid_show  && !_after_show && !_rs_show  ){
				if(!hybrid) return message.then(f=>f.warning('请确认是否杂交完成'))
				else onFinish(param, true)
			}else if( _hybrid_show  && _after_show && !_rs_show  ){
				if(!after) return message.then(f=>f.warning('请确认是否后处理完成'))
				else onFinish(param, true)
			}else if(_rs_show){
				onFinish( { ...param, content:{operation_44018, ...params}} )
			}
		}else if(mode === 44031){
			let { ctc_total, ctc_single, ctc_double, ctc_microemboli, ctc_microemboli_cell } = $fn.getBody(params)
			
			if( ctc_total === '' && ctc_single === '' && ctc_double === '' ){
				
			}else{
				if( +ctc_total !== (+ctc_single) + (+ctc_double) || ctc_total === '' || ctc_single === '' || ctc_double === ''){
					return message.then(f=>f.warning('检测数据①必须等于②+③的值', 3))
				}
			}
			
			if( (ctc_microemboli === '' && ctc_microemboli_cell === '') || ctc_microemboli === '/' || ctc_microemboli_cell === '/' ){
				
			}else{
				if( +ctc_microemboli < +ctc_microemboli_cell || ctc_microemboli === '' || ctc_microemboli_cell === ''){
					return message.then(f=>f.warning('微栓子簇数必须大于等于细胞数', 3)) 
				} 
			}
			
			onFinish( { content:{...params} }, true )
		}
		
	},[ mode, info, onFinish, 
		_result_show, _shou_show, harvest, inoculate,					// 44015
		_hybrid_show, _after_show, _rs_show, before, hybrid, after,		// 44018
		
	])
	const _props = { wrapClassName:'mb10', disabled, rows:4}
	
	return (
		<Box className='h' flex scroll full title='实验结果' scrollClassName='plr10 pb10' style={{width:mode===44031?430:320}} controls={controls()} loading={submitLoading}>
			<Form onSubmit={onSubmit} className='submit-form small-form  middle-form submit-item-form' ref={formRef} dir='x'>
				<IsUpload info={info} disabled={disabled} multiple/>
				{
					mode === 44015 && (
						<>
							<FormItem title='接种' value={inoculate} type='checkbox' label='是否接种完成' disabled={disabled || _shou_show} onChange={value=>{
								setInoculate(value)
							}}/>
							{
								_shou_show && (
									<FormItem title='收获' value={harvest} type='checkbox' label='是否收获完成' disabled={disabled || _result_show}  onChange={value=>{
										setHarvest(value)
									}}/>
								)
							}
							{
								_result_show && (
									<>
										<Title title='结果' />
										<FormItem tit='接种人' 		name='vaccinate_real_name' 		{..._props} disabled={true}/>
										<FormItem tit='接种时间' 	name='vaccinate_at'   			{..._props} disabled={true} type='date-time'/>
										<FormItem tit='收获人'		name='harvest_real_name'		{..._props} disabled={true}/>
										<FormItem tit='收获时间' 	name='harvest_at'				{..._props} disabled={true} type='date-time'/>
										<FormItem tit='条带水平' 	name='take_level'				{..._props} r />
										<FormItem tit='初步结果' 	name='preliminary_result' 		{..._props}	mode='textarea' r />
										<FormItem tit='记数细胞数' 	name='count_cells'				{..._props} mode='number' r />
										<FormItem tit='分析细胞数' 	name='analyze_cells'			{..._props} mode='number' r />
										<FormItem tit='拍摄细胞数' 	name='shoot_cells'				{..._props} mode='number' r />
										<FormItem tit='检验诊断' 	name='test_diagnosis'			{..._props} mode='textarea' r/>
										<FormItem tit='结果解释' 	name='result_interpretation'	{..._props} mode='textarea' r form={formRef} search={{ i:1, p:{type:2, kind_id: info.kind_id} }}/>
										<FormItem tit='建议其他检测' name='other_tests'				{..._props} mode='textarea'/>
									</>
								)
							}
						</>
					)
				}
				{
					mode === 44018 && (
						<>
							<FormItem title='前处理'  value={before} type='checkbox' label='是否前处理完成' wrapClassName='mt10' titleStyle={{padding:0}}  disabled={disabled || _hybrid_show}  onChange={value=>{
								setBefore(value)
							}}/>
							{
								_hybrid_show && (
									<FormItem title='杂交' value={hybrid} type='checkbox' label='是否杂交完成' titleStyle={{padding:0}} disabled={disabled || _after_show}  onChange={value=>{
										setHybrid(value)
									}}/>
								)
							}
							{
								_after_show && (
									<FormItem title='后处理' value={after} type='checkbox' label='是否后处理完成' titleStyle={{padding:0}} disabled={disabled || _rs_show}  onChange={value=>{
										setAfter(value)
									}}/>
								)
							}
							{
								_rs_show && (
									<>
										<Title title='结果' />
										<FormItem tit='探针名称' 	name='probe_name'				{..._props} r />
										<FormItem tit='分析细胞数' 	name='analysis_cell_number'		{..._props} mode='number' r />
										<FormItem tit='病理诊断' 	name='pathological_diagnosis'	{..._props} mode='textarea' form={formRef} search={{ i:1, p:{type:1, kind_id: info.kind_id} }}/>
										<FormItem tit='蜡块/玻片号' 	name='wax_stone'				{..._props} />
										<FormItem tit='结果' 		name='result'					{..._props} r />
										<FormItem tit='检测结果' 	name='testing_result'			{..._props} mode='textarea' r form={formRef} search={{ i:1, p:{type:1, kind_id: info.kind_id} }}/>
										<FormItem tit='建议与解释' 	name='result_suggestion_name'	{..._props} mode='textarea' form={formRef} search={{ i:1, p:{type:2, kind_id: info.kind_id} }}/>
									</>
								)
							}
						</>
					)
				}
				{
					mode === 44031 && (
						<>
							<FormItem title='送检目的' name='purpose_inspec' disabled={disabled} type='checkboxGroup' r
								data={
									[
										{label:'辅助诊断', value:'1'},
										{label:'术后复发转移高风险预测', value:'2'},
										{label:'疗效评价或肿瘤发展监控', value:'3'}
									] 
								}
							/>
							<Title title='检测数据（①=②+③）' />
							<ul className='f12 line_input'>
								<li className='fxm'><span className='mr5 f13 rel' style={{top:-1}}>①</span><span>共检查到单个CTC</span><FormItem name='ctc_total' type='html-input' disabled={disabled} /><span>个;</span></li>
								<li className='fxm'><span className='mr5 f13 rel' style={{top:-1}}>②</span><span>单染色体异常</span><FormItem name='ctc_single' type='html-input' disabled={disabled} /><span>个;</span></li>
								<li className='fxm'><span className='mr5 f13 rel' style={{top:-1}}>③</span><span>双染色体异常</span><FormItem name='ctc_double' type='html-input' disabled={disabled} /><span>个;</span></li>
								<li className='fxm'>
									<span className='mr5 f13 rel' style={{top:-1}}>④</span>
									<span>微栓子</span><FormItem name='ctc_microemboli' type='html-input' disabled={disabled} /><span>簇，</span>
									<span>共</span><FormItem name='ctc_microemboli_cell' type='html-input' disabled={disabled} /><span>个细胞。</span>
								</li>
								<li className='fxm'>
									<span style={{width:80}}>探针A：</span>
									<span>三体</span><FormItem name='cep8_3' type='html-input' disabled={disabled} /><span>个，</span>
									<span>四体</span><FormItem name='cep8_4' type='html-input' disabled={disabled} /><span>个，</span>
									<span>多体</span><FormItem name='cep8_multi' type='html-input' disabled={disabled} /><span>个，</span>
								</li>
								<li className='fxm'>
									<span style={{width:80}}>探针B：</span>
									<span>三体</span><FormItem name='cep7_3' type='html-input' disabled={disabled} /><span>个，</span>
									<span>四体</span><FormItem name='cep7_4' type='html-input' disabled={disabled} /><span>个，</span>
									<span>多体</span><FormItem name='cep7_multi' type='html-input' disabled={disabled} /><span>个，</span>
								</li>
								<li className='fxm'>
									<span style={{width:80}}>探针A+B：</span>
									<span>三体</span><FormItem name='cep17_3' type='html-input' disabled={disabled} /><span>个，</span>
									<span>四体</span><FormItem name='cep17_4' type='html-input' disabled={disabled} /><span>个，</span>
									<span>多体</span><FormItem name='cep17_multi' type='html-input' disabled={disabled} /><span>个，</span>
								</li>																					
							</ul>
							<FormItem title='检测结果' 	name='result'	{..._props} mode='textarea' r/>
						</>
					)
				}
			</Form>
		</Box>
	)
}

export default Index